/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <memory>

#include "common/libs/confui/confui.h"

/** ConfUiUserSelectionMessage with a security flag
 *
 * Inputs generated by something that belong to (virtualized) TEE is regarded
 * as secure. Otherwise (e.g. inputs generated by the guest calling
 * deliverSecureInputEvent), it is regarded as insecure.
 *
 * The host marks the security field, and use it internally and exclusively.
 *
 */
namespace cuttlefish {
namespace confui {
class ConfUiSecureUserSelectionMessage : public ConfUiMessage {
 public:
  ConfUiSecureUserSelectionMessage(
      std::unique_ptr<ConfUiUserSelectionMessage>&& msg, const bool secure);
  ConfUiSecureUserSelectionMessage() = delete;
  virtual ~ConfUiSecureUserSelectionMessage() = default;
  std::string ToString() const override { return msg_->ToString(); }
  ConfUiCmd GetType() const override { return msg_->GetType(); }
  auto GetResponse() const { return msg_->GetResponse(); }
  // SendOver is between guest and host, so it doesn't send the is_secure_
  bool SendOver(SharedFD fd) override { return msg_->SendOver(fd); }
  bool IsSecure() const { return is_secure_; }
  // SetSecure() might be needed later on but not now.

 private:
  std::unique_ptr<ConfUiUserSelectionMessage> msg_;
  bool is_secure_;
};

class ConfUiSecureUserTouchMessage : public ConfUiMessage {
 public:
  ConfUiSecureUserTouchMessage(std::unique_ptr<ConfUiUserTouchMessage>&& msg,
                               const bool secure);
  virtual ~ConfUiSecureUserTouchMessage() = default;
  std::string ToString() const override { return msg_->ToString(); }
  ConfUiCmd GetType() const override { return msg_->GetType(); }
  auto GetResponse() const { return msg_->GetResponse(); }
  bool SendOver(SharedFD fd) override { return msg_->SendOver(fd); }
  std::pair<int, int> GetLocation() const { return msg_->GetLocation(); }
  bool IsSecure() const { return is_secure_; }

 private:
  std::unique_ptr<ConfUiUserTouchMessage> msg_;
  bool is_secure_;
};

std::unique_ptr<ConfUiSecureUserSelectionMessage> ToSecureSelectionMessage(
    std::unique_ptr<ConfUiUserSelectionMessage>&& msg, const bool secure);
std::unique_ptr<ConfUiSecureUserTouchMessage> ToSecureTouchMessage(
    std::unique_ptr<ConfUiUserTouchMessage>&& msg, const bool secure);
}  // end of namespace confui
}  // end of namespace cuttlefish
